package nqueens.algorithm;

import java.util.Iterator;
import java.util.Random;

import nqueens.config.GAProperties;
import nqueens.core.Chromosome;
import nqueens.core.Population;

/**
 * Class that implements the mutation operator
 * 
 * @author Artur Alves
 * @author Danilo Torres
 * @author Rodrigo Bruno
 * 
 * 
 */
public class MutatorDefault implements IMutationAlgorithm {
	@Override
	public void mutate(Population nextGeneration) {
		Random r = new Random();
		double mutationRate = GAProperties.getInstance().getDouble(GAProperties.MUTATION_RATE);
		int numQueens = GAProperties.getInstance().getInteger(GAProperties.NUM_QUEENS);
		Iterator<Chromosome> it = nextGeneration.iterator();
		int counter = 0;
		int total = 0;
		while (it.hasNext()) {
			Chromosome c = it.next();
			int[] values = c.getValues();
			for (int i = 0; i < values.length; i++) {
				double prob = r.nextDouble();
				if (prob < mutationRate) {
					values[i]++;
					values[i] %= numQueens;
					counter++;
				}
				total++;
			}
		}
	}
}
